//
//  AnnouncerTableViewCell.m
//  MountEverestFM
//
//  Created by ChenNing on 15/11/2.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "AnnouncerTableViewCell.h"
#import "AnnCollectionViewCell.h"

@interface AnnouncerTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView *annCollectionView;
@end

@implementation AnnouncerTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UICollectionViewFlowLayout *annFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        annFlowLayout.itemSize = CGSizeMake((FM_Screen_Width-FM_Screen_Pts_Width*40)/3,(FM_Screen_Width-FM_Screen_Pts_Width*40)/3+FM_Screen_Pts_Height*50);
        annFlowLayout.sectionInset = UIEdgeInsetsMake(FM_Screen_Pts_Height*10, FM_Screen_Pts_Height*10, FM_Screen_Pts_Height*10, 0);
        annFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.annCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:annFlowLayout];
        self.annCollectionView.dataSource = self;
        self.annCollectionView.delegate = self;
        self.annCollectionView.showsHorizontalScrollIndicator = NO;
        self.annCollectionView.backgroundColor = [UIColor whiteColor];
        self.annCollectionView.alwaysBounceVertical = NO;
        [self.contentView addSubview:_annCollectionView];
        
        [self.annCollectionView registerClass:[AnnCollectionViewCell class] forCellWithReuseIdentifier:@"annShowCell"];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.annCollectionView.frame = CGRectMake(0, 0, FM_Screen_Width, (FM_Screen_Width-FM_Screen_Pts_Width*40)/3+FM_Screen_Pts_Height*70);
}

// UICollectionViewDataSource 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.announcerArray.count;
}

- (void)setAnnouncerArray:(NSArray *)announcerArray
{
    if (_announcerArray != announcerArray) {
        _announcerArray = announcerArray;
    }
    [self.annCollectionView reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AnnCollectionViewCell *announcerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"annShowCell" forIndexPath:indexPath];
    announcerCell.annDictionary = [self.announcerArray objectAtIndex:indexPath.row];
    [announcerCell.imageViewButton addTarget:self action:@selector(selectItemAction:) forControlEvents:UIControlEventTouchUpInside];
    announcerCell.imageViewButton.tag = 10400 + indexPath.row;
    return announcerCell;
}

- (void)selectItemAction:(UIButton *)sender
{
    NSInteger index = sender.tag-10400;
    [self.delegate topPage:[self.announcerArray objectAtIndex:index]];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate topPage:[self.announcerArray objectAtIndex:indexPath.row]];
}

@end
